package com.ojoin.trade.supplyloan_core.web.oa;

import org.activiti.engine.IdentityService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.identity.User;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ojoin.trade.bpm.web.support.BpmUserEntityService;
import com.ojoin.trade.common.utils.LogUtils;
@Service
public class RejectMailNotice implements ExecutionListener {

	private final Logger log = LoggerFactory.getLogger(this.getClass()); 
	
	@Autowired
	private BpmUserEntityService bpmUserEntityService;
	
	@Override
	public void notify(DelegateExecution execution) throws Exception {
		IdentityService identityService = execution.getEngineServices().getIdentityService();
        String ywzyUserId = (String) execution.getVariable("retrial_initiator");
        User user = bpmUserEntityService.findUserById(ywzyUserId);
        String to = "hgc@18join.com";
        String userName = "MailNotice-but-userNotFind-"+execution.getProcessBusinessKey();
        if(null!=user){
        	userName = user.getFirstName() + " " + user.getLastName();
        	if(StringUtils.isNotEmpty(user.getEmail())&&user.getEmail().indexOf("@")>0){
        		to = user.getEmail();
        	}else{
        		LogUtils.info(log, "user {} has not set mail",userName);
        	}
        }
        execution.setVariableLocal("to", to);
        execution.setVariableLocal("name", userName);
	}

}
